#include<bits/stdc++.h>
#define i64 long long
#define ull unsigned long long
using namespace std;

const int N = 1e6 + 10, INF = 0x3f3f3f3f;


void solve()
{
    int n;
    cin >> n;
    vector<i64> p1(n + 1), p0(n + 1), p(n + 1);
    string s;
    cin >> s;
    s = " " + s;
    i64 ans = 0;
    for (int i = 1; i <= n; i++) {
        p1[i] = p1[i - 1] + (s[i] == '1');
        p0[i] = p0[i - 1] + (s[i] == '0');
        p[i] = p[i - 1] + (s[i] == '?');
        if (s[i] == '0' || s[i] == '?') ans += p1[i];
    }

    i64 res = ans;

    for (int i = 1; i <= n; i++) {
        if (s[i] == '?') {
            res -= (p1[i] + p[i - 1]);
            i64 fs0 = p0[n] + p[n] - p[i] - p0[i];
            res += fs0;
            ans = max(res, ans);
        }
    }
    cout << ans << "\n";
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    cin >> T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}